package com.baoxin.chenxixc4coach;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class SendMsgActivity extends BaseActivity {

    private EditText mContent;
    private RadioGroup mRG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_msg);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mContent = findViewById(R.id.msg_content);
        mRG = findViewById(R.id.radio_group);
        mRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        mContent.setText("您已经预约了***的练习，请按时参加！");
                        break;
                    case R.id.rb2:
                        mContent.setText("您已经完成了***的练习，请及时评价！");
                        break;
                }
            }
        });

    }

    /**
     * 立即发送
     *
     * @param view
     */
    public void onSend(View view) {
        String content = mContent.getText().toString();
        if (content.length() == 0) {
            Toast.makeText(this, "请输入短信内容！", Toast.LENGTH_SHORT).show();
            return;
        }

        checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

    }

    private void checkPermission(String permission) {
        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{permission}, 1);

        } else {
            sendNoticeMessage();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            sendNoticeMessage();
        } else {
            showMissingPermissionDialog();
        }
    }

    //todo---------------
    private void sendNoticeMessage() {

    }
}
